package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.AgencyRecordAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PageBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.OnLoadeMoreListener;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class AgencyRecordActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.btn_go)
    SuperButton btnGo;
//    @BindView(R.id.tablayout)
//    TabLayout tablayout;
//    @BindView(R.id.pager)
//    ViewPager pager;
//    @BindView(R.id.btn_submit)
//    TextView btnSubmit;

    private Unbinder unbiner;
    private AgencyRecordAdapter adapter;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getOilcardAgencyList(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agency_record);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("代办记录");
        txtRight.setText("客服");
        txtRight.setVisibility(View.VISIBLE);
        adapter = new AgencyRecordAdapter(this);
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getOilcardAgencyList(1);
            }
        });
        getOilcardAgencyList(1);
    }

    public void getOilcardAgencyList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        PageBean bean = new PageBean();
        bean.page = page;
        loadingDialog.show();
        Api.api_service.getOilcardAgencyList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                        btnSubmit.setVisibility(View.GONE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                        btnSubmit.setVisibility(View.VISIBLE);
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_go, R.id.btn_back, R.id.btn_submit, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Util.startActivity(this, ChatCenterActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                Util.startActivity(this, OilAgencyActivity.class);
                break;
            case R.id.btn_go:
                Util.startActivity(this, OilAgencyActivity.class);
                break;
        }
    }
}
